﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InSafetyAirClient.GUI.AccountPerson
{
    public partial class ListAP : UserControl
    {
        List<InSafetyAirClient.InSafetyAirService.Person> pers = null;
        List<InSafetyAirClient.InSafetyAirService.Account> accs = null;

        public ListAP()
        {
            InitializeComponent();
        }

        private void ListAP_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                populateList();
            }
        }

        private void populateList()
        {
            DataTable dataTable1 = new DataTable();
            dataTable1.Columns.Add("F Name", typeof(string));
            dataTable1.Columns.Add("S Name", typeof(string));
            dataTable1.Columns.Add("Gender.", typeof(string));
            dataTable1.Columns.Add("Passport Nr", typeof(string));
            dataTable1.Columns.Add("Address", typeof(string));
            dataTable1.Columns.Add("Phone", typeof(string));
            dataTable1.Columns.Add("Username", typeof(string));
            dataTable1.Columns.Add("Email", typeof(string));
            dataTable1.Columns.Add("Access", typeof(string));
            ServiceSingle servS = ServiceSingle.Instance;
            pers = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Person).Name).OfType<InSafetyAirClient.InSafetyAirService.Person>().ToList();
            accs = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Account).Name).OfType<InSafetyAirClient.InSafetyAirService.Account>().ToList();
            foreach (InSafetyAirService.Person person in pers)
            {
                bool hasacc = false;
                foreach (InSafetyAirService.Account acc in accs)
                {
                    if (acc.personId == person.personId)
                    {
                        hasacc = true;
                        dataTable1.Rows.Add(person.fName,person.sName,person.gender,person.passportNo,person.address,person.phone,acc.username,acc.email,acc.access);
                    }
                }
                if (!hasacc)
                {
                    dataTable1.Rows.Add(person.fName, person.sName, person.gender, person.passportNo, person.address, person.phone,"•","•","•");
                }
            }
            dataGridView1.DataSource = dataTable1;
        }

    }
}
